/**
 * ...
 * @author Hatshepsout
 1 A4 = 5 watts
 1 A3 = 10 watts
1 alimentation au minimum dans une vitrine
calculer le nombre
pour les gammes c2.max et i.max fixation cable bas en haut
gamme one et budget entre les afficheurs -> mini cables trois : longueurs différentes + cable de fixation en haut
wall format = tout est posé sur un mur
 */
// it is a DOM element controller who can dispatchEvent according to the Tomahawk's EventDispatcher API

function ColorPicker()
{
	tomahawk_ns.EventDispatcher.apply(this);
}

Tomahawk.registerClass(ColorPicker,"ColorPicker");
Tomahawk.extend("ColorPicker","EventDispatcher");

ColorPicker.prototype._canvas = null;
ColorPicker.prototype._container = null;
ColorPicker.prototype.width = 1;
ColorPicker.prototype.height = 1;
ColorPicker.prototype._currentColor = "black";
ColorPicker.prototype._deployed = false;
ColorPicker.prototype._boxSize = 20;

ColorPicker.CHANGE_COLOR_EVENT = "changeColorEvent";


ColorPicker.prototype.init = function(container,width,height)
{
	var scope = this;
	var callback = function(event){ scope._clickHandler(event); };
	this._container = container;
	this._canvas = document.createElement("canvas");
	this._canvas.addEventListener("click", callback );
	this.width = width;
	this._canvas.style.border = "1px solid black";
	this.height = height;
	
	this._container.appendChild(this._canvas);
	this.refresh();
};

ColorPicker.prototype.getColor = function()
{
	return this._currentColor;
};

ColorPicker.prototype._clickHandler = function(event)
{
	var context = this._canvas.getContext("2d");
	var bounds = this._canvas.getBoundingClientRect();
	var x = parseInt(event.clientX - bounds.left);
	var y = parseInt(event.clientY - bounds.top);
	
	var data = context.getImageData(x,y,1,1).data;
	
	if( this._deployed == true )
	{
		var r = data[0].toString(16);
		var g = data[1].toString(16);
		var b = data[2].toString(16);
		var a = data[3].toString(16);
		
		r = ( r.length == 1 ) ? "0"+r : r;
		g = ( g.length == 1 ) ? "0"+g : g;
		b = ( b.length == 1 ) ? "0"+b : b;
		a = ( a.length == 1 ) ? "0"+a : a;
		this._currentColor = "#"+r+g+b;
		
		this.dispatchEvent(new tomahawk_ns.Event(ColorPicker.CHANGE_COLOR_EVENT,true,true));
	}

	this._deployed = !this._deployed;
	this.refresh();
};

ColorPicker.prototype.setColor = function(value)
{
	this._currentColor = value;
	this.refresh();
};

ColorPicker.prototype.refresh = function()
{
	var context = this._canvas.getContext("2d");
	
	if( this._deployed == true )
	{	
		this._canvas.width = this.width;
		this._canvas.height = this.height;
		var texture = tomahawk_ns.AssetsManager.getInstance().getTextureByAlias("picker");
		context.clearRect(0,0,this._canvas.width,this._canvas.height);
		context.drawImage(	texture.data,
							texture.rect[0],
							texture.rect[1],
							texture.rect[2],
							texture.rect[3],
							0,
							0,
							this.width,
							this.height
							);
	}
	else
	{
		this._canvas.width = this._canvas.height = this._boxSize;
		context.clearRect(0,0,this._canvas.width, this._canvas.height);
		context.beginPath();
		context.fillStyle = this._currentColor;
		context.fillRect(0,0,this._boxSize,this._boxSize);
		context.fill();
	}
};